package com.hmyd.hmyd_android.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.BillRedeemData;

/**
 * 赎回列表适配
 * @author wangk
 */
public class BillRedeemListAdapter extends BaseAdapter {

	private List<BillRedeemData> datas;
	private Context context;
	private Map<String, String> map;
	private Button bill_redeem_confirm;
	private boolean canRedeem;
	private Map<String, String> errorMap;
	
	public BillRedeemListAdapter(List<BillRedeemData> datas, Context context,Button bill_redeem_confirm,boolean canRedeem) {
		super();
		this.datas = datas;
		this.context = context;
		this.bill_redeem_confirm = bill_redeem_confirm;
		this.canRedeem = canRedeem;
		errorMap = new HashMap<String, String>();
		map = new HashMap<String, String>();
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position%getCount());
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public Map<String, String> getMap() {
		return map;
	}
	
	public void showError(Map<String, String> errorMap) {
		this.errorMap = errorMap;
		notifyDataSetInvalidated();
	}
	
	public Map<String, String> getErrorMap() {
		return errorMap;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if(convertView ==null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.redeem_item, null);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.bill_money_input = (EditText) convertView.findViewById(R.id.bill_money_input);
			holder.redeem_value = (TextView) convertView.findViewById(R.id.redeem_value);
			holder.error_text = (TextView) convertView.findViewById(R.id.error_text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final BillRedeemData data = datas.get(position);
		holder.redeem_value.setText(data.purchasedAmount);
		holder.title.setText(data.title);
		holder.bill_money_input.setTag(data.id);
		holder.bill_money_input.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if(holder.bill_money_input.getTag() == data.id){
					if(!TextUtils.isEmpty(s)) {
						map.put(data.id,s.toString());
					} else {
						if(map.containsKey(data.id)) {
							map.remove(data.id);
						}
					}
				}
//				if(errorMap.containsKey(data.id)) {
//					errorMap.remove(data.id);
//				}
			}
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}
			@Override
			public void afterTextChanged(Editable s) {
				
				if(canRedeem) {
					if(map.size() > 0) {
							bill_redeem_confirm.setClickable(true);
							bill_redeem_confirm.setEnabled(true);
					} else {
							bill_redeem_confirm.setClickable(false);
							bill_redeem_confirm.setEnabled(false);
					}
				}
			}
		});
		holder.error_text.setVisibility(View.GONE);
		Set<String> keys = errorMap.keySet();
		for (String string : keys) {
			if(string.equals(data.id)) {
				holder.error_text.setText(errorMap.get(data.id));
				holder.error_text.setVisibility(View.VISIBLE);
			}
		}
		
		return convertView;
	}
	
	private class ViewHolder {
		EditText bill_money_input; // 金额输入框
		TextView redeem_value; // 可赎回金额
		TextView title; // 项目标题
		TextView error_text; // 错误提示
	}
	
}
